package app.frames;

import app.actions.CancelAction;
import app.gui.JDate;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import libs.Attendance;
import libs.AttendanceException;
import libs.Seminar;
import libs.Student;
import libs.StudentSorter;

/**
 *  Dialog box for mass seminar attendance modification
 * @author Dejvino
 */
public class SeminarAttendanceForm extends javax.swing.JFrame {

    private Attendance attendance = null;

    private Seminar seminar = null;

    private CancelAction cancelAction;

    /** Creates new form SeminarAttendanceForm */
    public SeminarAttendanceForm(JFrame frame, Seminar seminar, Attendance attendance) {
        if(seminar == null)
            throw new NullPointerException("seminar");
        if(attendance == null)
            throw new NullPointerException("attendance");
        this.seminar = seminar;
        this.attendance = attendance;
        initComponents();

        cancelAction = new CancelAction(this, frame);

        // dnesni datum
        ((JDate)jDate).setDate(new Date());

        // nastaveni informaci o predmetu a seminari
        jSubject.setText(seminar.getSubject().toString());
        jSeminar.setText(seminar.toString());

        // pridani studentu do seznamu
        DefaultTableModel model = (DefaultTableModel)jStudents.getModel();
        for(Student s : new StudentSorter(attendance.getStudents(seminar)).getArray()) {
            model.addRow(new Object[]{s.getName(), new Integer(s.getId()), Boolean.TRUE});
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDate = new JDate();
        jScrollPane1 = new javax.swing.JScrollPane();
        java.util.ResourceBundle lang = java.util.ResourceBundle.getBundle("app/frames/language");
        jStudents = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPresent = new javax.swing.JCheckBox();
        jOK = new javax.swing.JButton();
        jCancel = new javax.swing.JButton();
        jSubject = new javax.swing.JTextField();
        jSeminar = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        jStudents.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                lang.getString("Name"), lang.getString("ID"), lang.getString("Select")
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jStudents);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("app/frames/language"); // NOI18N
        jLabel1.setText(bundle.getString("Subject")); // NOI18N

        jLabel2.setText(bundle.getString("Seminar")); // NOI18N

        jLabel3.setText(bundle.getString("Date")); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText(bundle.getString("StudentList")); // NOI18N

        jPresent.setSelected(true);
        jPresent.setText(bundle.getString("Attendance")); // NOI18N
        jPresent.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jPresent.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        jOK.setText(bundle.getString("OK")); // NOI18N
        jOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOKActionPerformed(evt);
            }
        });

        jCancel.setText(bundle.getString("Cancel")); // NOI18N
        jCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelActionPerformed(evt);
            }
        });

        jSubject.setEditable(false);
        jSubject.setText("jTextField1");

        jSeminar.setEditable(false);
        jSeminar.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                        .addComponent(jOK, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jDate, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPresent, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSubject, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                            .addComponent(jSeminar, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jSeminar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jPresent))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jOK)
                    .addComponent(jCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cancelAction.actionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    private void jCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelActionPerformed
        cancelAction.actionPerformed(null);
}//GEN-LAST:event_jCancelActionPerformed

    private void jOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOKActionPerformed
        // ziskani data
        Date date = ((JDate)jDate).getDate();
        boolean present = jPresent.isSelected();
        // ziskani oznacenych studentu
        DefaultTableModel model = (DefaultTableModel)jStudents.getModel();
        for(int row = model.getRowCount(); row > 0; row--) {
            // preskoceni nevybranych studentu
            if(!((Boolean)model.getValueAt(row-1, 2)).booleanValue())
                continue;
            // ziskani studenta
            Student student = new Student((String)model.getValueAt(row-1, 0),
                    ((Integer)model.getValueAt(row-1, 1)).intValue());
            try {
                if(present)
                    attendance.present(student, seminar, date);
                else
                    attendance.absent(student, seminar, date);
            } catch (AttendanceException ex) {
                Logger.getLogger(SeminarAttendanceForm.class.getName()).log(Level.WARNING, null, ex);
            }
        }

        cancelAction.actionPerformed(evt);
}//GEN-LAST:event_jOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancel;
    private javax.swing.JFormattedTextField jDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jOK;
    private javax.swing.JCheckBox jPresent;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jSeminar;
    private javax.swing.JTable jStudents;
    private javax.swing.JTextField jSubject;
    // End of variables declaration//GEN-END:variables

}
